package cn.pzhuweb.controller;

import cn.pzhuweb.pojo.Message;
import cn.pzhuweb.pojo.User;
import cn.pzhuweb.service.imp.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Servlet implementation class TodoServlet
 */

public class ChangePswServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangePswServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        String newusername = request.getParameter("newpassword");
        String repassword = request.getParameter("repassword");
        User user = new User(username, newusername);
        UserServiceImp us = new UserServiceImp();
        Message message = us.changePassword(user, repassword);
        if (message.isSuccess()) {
            request.getSession().setAttribute("user", user);
            response.getWriter().print("<script>alert('" + message.getMsg() + "');window.location.href='success.jsp'</script>");
        } else {
            response.getWriter().print("<script>alert('" + message.getMsg() + "');window.location.href='changepsw.jsp'</script>");
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
